package com.cpi.cornpp.transitcorn.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.poplanned.reals.services.PoPlannedRealsService;
import com.cpi.cornpp.poplanned.reals.view.PoPlannedRealsView;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderView;
import com.cpi.cornpp.transitcorn.services.TransitCornService;

/**
 * 
 * Transit Corn  Action
 * @author Edgar Joao
 *
 */
public class TransitCornAction extends Action {
	/** Default */
	private final static String DEFAULT = "default";//$NON-NLS-1$
	
	
	/**
	 * List of Transit Corn
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		TransitCornService transitCornService = null;
		PoPlannedRealsService poPlannedRealsService = null;
		PurchaseProgramService purchaseProgramService = null;
		
		PoPlannedRealsView poPlannedRealsView = null;
		String purchaseOrderId = null;
		String poNumberParameter = null;
		List transitCornList = null;
		PurchaseOrderView purchaseOrderView = null;
				
		transitCornService = new TransitCornService();
		poPlannedRealsService = new PoPlannedRealsService();
		purchaseProgramService  = new PurchaseProgramService();
		
		transitCornList = new ArrayList(0);
		poPlannedRealsView = new PoPlannedRealsView();
		
		purchaseOrderId = request.getParameter("purchaseOrderId");
		
		poPlannedRealsView.setPurchaseOrderId(Integer.parseInt(purchaseOrderId));		
		poNumberParameter = poPlannedRealsService.getPoPlannedRealsById(poPlannedRealsView).getPoNumber();
		purchaseOrderView = purchaseProgramService.getPurchaseOrderById(Integer.parseInt(purchaseOrderId));
		
		transitCornList = transitCornService.list(Integer.parseInt(purchaseOrderId));
		
		if(poNumberParameter != null){
			request.setAttribute("poNumberParameter", poNumberParameter);
		}else {
			request.setAttribute("poNumberParameter", StringUtils.EMPTY);
		}
		
		request.setAttribute("purchaseOrderView", purchaseOrderView);
		request.setAttribute("transitCornList", transitCornList);
						
		return mapping.findForward(DEFAULT);
	}

}
